//
//  TravelTopicMoreViewController.m
//  ToWhere
//
//  Created by dllo on 15/11/14.
//  Copyright © 2015年 www.TE.com. All rights reserved.
//

#import "TravelTopicMoreViewController.h"
#import "DataAnalysis.h"
#import "TravelTopicModel.h"
#import <MJRefresh.h>
#import "TravelTopicTableView.h"
#import "TeavelTopicDetailsViewController.h"

#define Travel_Topic_URL @"http://chanyouji.com/api/articles.json?page="
@interface TravelTopicMoreViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic, retain) UIView *titleView;
@property (nonatomic, retain) NSMutableArray *array;
@property (nonatomic, assign) NSInteger pageNumber;
@property (nonatomic, retain) UITableView *tableView;
@end

@implementation TravelTopicMoreViewController

-(void)dealloc
{
    [_array release];
    [_tableView release];
    [_titleView release];
    
    [super dealloc];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.array = [NSMutableArray array];
    // Do any additional setup after loading the view.
    
    self.pageNumber = 1;
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    //网路请求
    [self loading];
    //创建标题
    [self createTitle];
    //创建tableview
    [self createTableView];
}

#pragma mark 创建tableView
-(void)createTableView
{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64 *kHeight, self.view.frame.size.width, self.view.frame.size.height - 64 *kHeight) style:UITableViewStylePlain];
    self.tableView.delegate =self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [_tableView release];
    
    [self.tableView registerClass:[TravelTopicTableView class] forCellReuseIdentifier:@"TravelTopicTableViewCell"];
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    
}
#pragma 上拉刷新
-(void)loadNewData
{
    [self.array removeAllObjects];
    NSLog(@"%ld",self.array.count );
    
    self.pageNumber = 1;
    [self loading];
    [self.tableView reloadData];
}
#pragma 下拉加载
-(void)loadMoreData
{
    self.pageNumber += 1;
    [self loading];
    [self.tableView.footer noticeNoMoreData];
}
#pragma mark 创建标题
-(void)createTitle
{
    self.view.backgroundColor = [UIColor purpleColor];
    
    self.titleView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,kHeight * 64)];
    self.titleView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:self.titleView];
    [self.titleView release];
    [_titleView release];
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(15 * kWidth, 25 * kHeight, 20 * kWidth, 20 * kHeight);
    [backBtn setBackgroundImage:[UIImage imageNamed:@"Food_back"] forState:UIControlStateNormal];
    [self.titleView addSubview:backBtn];
    [backBtn addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 30 * kWidth, self.titleView.frame.size.height / 2 - 10 * kHeight, kWidth * 60, kHeight * 30)];
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.text =  @"专题";
    titleLabel.font = [UIFont systemFontOfSize:17 * kWidth];
    [self.titleView addSubview:titleLabel];
    [titleLabel release];
}

//返回点击事件
-(void)backAction:(UIButton *)btn
{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

#pragma mark网络请求
-(void)loading
{
    [DataAnalysis dataAnalysisGETWithUrlString:[NSString stringWithFormat:@"%@%ld",Travel_Topic_URL,self.pageNumber] parameters:nil showHuD:YES hiddenView:nil successfulBlock:^(id responseObject) {
        NSArray * arr = responseObject;
               for (NSDictionary *dic in arr) {
            
            TravelTopicModel *model = [TravelTopicModel travelTopicModelinitWithNSDictionary:dic];
            
            [self.array addObject:model];
        }
        [self.tableView.header endRefreshing];
        [self.tableView.footer endRefreshing];
        [self.tableView reloadData];
    
    } failureBlock:^(NSError *error) {
        
        NSLog(@"%@", error);
        
    }];
    
   
}
//返回cell个数
-(NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _array.count;
}
//返回行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 200 *kHeight;
}
//返回cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    TravelTopicTableView *cell = [tableView dequeueReusableCellWithIdentifier:@"TravelTopicTableViewCell"forIndexPath:indexPath];
    

    if (self.array.count != 0) {
        cell.topicModel = self.array[indexPath.row];

    }
    
        cell.selectionStyle =UITableViewCellSelectionStyleNone;

    if (self.array.count != 0 ) {
        cell.topicModel = self.array[indexPath.row];
    }
    cell.selectionStyle =UITableViewCellSelectionStyleNone;

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
#pragma mark cell点击事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   
    TeavelTopicDetailsViewController *view = [[TeavelTopicDetailsViewController alloc]init];
    
    view.idstring = [self.array[indexPath.row] ID];
    
    [self setHidesBottomBarWhenPushed:YES];
    
    [self.navigationController pushViewController:view animated:YES];
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
